var t = require("../../components/wux"), e = function(t) {
    return t && t.__esModule ? t : {
        default: t
    };
}(require("../../asset/plugins/WxValidate"));
var api = require("../../api.js"),
  app = getApp();
var r;
Page({
    data: {
        tel: "",
        agree:false
    },
    onLoad: function() {
        this.initValidate();
        r = this;
    },
    onShow:function(){
      var that = this;
      r.toast = that.selectComponent("#toast");
    }, 
    showToptips: function(e) {
        wx.showToast({
          title: '成功',
          icon: 'success',
          duration: 2000
        })
        // t.$wuxToptips.show({
        //     timer: 3e3,
        //     text: e.msg || "请填写正确的字段",
        //     success: function() {
        //         return console.log("toptips", e);
        //     }
        // });
    },
    userNameTel: function(t) {
        this.setData({
            tel: t.detail.value
        });
    },
    submitForm: function(t) {
        var e = t.detail.value;
        if (console.log(e), !this.WxValidate.checkForm(t)) {
          debugger
            var o = this.WxValidate.errorList[0];
            wx.showToast({
              title: o.msg,
              image: '../../asset/img/warn.png',
              duration: 2000
            })
            return
        }
        if(!this.data.agree) {
          wx.showToast({
            title: '请勾选同意使用协议',
            image: '../../asset/img/warn.png',
            duration: 2000
          })
         return;
        }
        var n = new Object();
        var user = getApp().globalData.wxUserInfo;
        // n.phone = e.tel, n.openId = user.openId,
        n.phone = e.tel,n.code=e.code;
        console.log(n)
        wx.request({
            url:api.login.BindPhoneLogin ,
              data:n,
              method: "POST",
              header: {
                  "content-type": "application/x-www-form-urlencoded"
              },
            success: function(res) {
              if (res.statusCode == 200&&res.data.code==200) {
                  if(res.data.code==200){
                    wx.showToast({
                        title: '绑定成功'
                      })
                      setTimeout(function(){
                        var pages = getCurrentPages(); //获取页面栈
                      if (pages.length > 1) {
                        //上一个页面实例对象
                        var prePage = pages[pages.length - 2];
                        //调用上一个页面的onShow方法
                        prePage.onLoad();
                        wx.navigateBack({
                          delta: 1
                        })
                      }
                      },2000)
                  }else{
                      r.toast.showToast(res.data.msg);
   
                  }
           
              }
            }
          });
    },
    vcode: function() { 
        var e = {}, o = this;
        if ("" == this.data.tel) {
          wx.showToast({
            title: '请输入手机号码',
            image: '../../asset/img/warn.png',
            duration: 2000
          })
          return;
        }
        if (this.data.tel.length < 11) {
          wx.showToast({
            title: '请输入正确的手机号',
            image: '../../asset/img/warn.png',
            duration: 2000
          })
          return;
        }
        var n = new Object();
        var user = getApp().globalData.wxUserInfo;
        n.phone = this.data.tel, n.openId = user.openId,
        wx.request({
            url:api.login.checkBindPhone ,
            data: { open_id: user.openId, phone: this.data.tel},
            success:function(data){
              console.log(data)
              if (data.data.code === 200) {
                wx.showToast({
                  title: '发送成功',
                  icon: 'success',
                  duration: 2000
                })
              }else if(data.data.code === 500) {
                wx.showToast({
                  title: data.data.msg,
                  image: '../../asset/img/warn.png',
                  duration: 2000
                })
              }
            }
        })

    },
    initValidate: function() {
        var t = this;
        this.WxValidate = new e.default({
            tel: {
                required: !0,
                tel: !0
            },
            code: {
                required: !0,
                code: !0
            }
        }, {
            tel: {
                required: "请输入手机号",
                tel: "请输入正确的手机号"
            },
            code: {
                required: "请输入验证码"
            }
        }), this.WxValidate.addMethod("code", function(e, i) {
            return t.WxValidate.optional(e) || 6 == e.length;
        }, "请输入6位数的验证码");
    },
    agree:function(e){
        var that = this;
        console.log(e)
        var type = e.currentTarget.dataset.type;
        if(type==2){
         that.setData({
          agree:true
         })
        }else{
          that.setData({
            agree:false
           })
        }
        console.log(that.data)
      },
});